#include <linux/io.h>
#include <linux/clk.h>
#include <linux/gpio.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_device.h>
#include <linux/pinctrl/consumer.h>
#include <linux/pinctrl/machine.h>
#include <linux/pinctrl/pinctrl.h>
#include <linux/pinctrl/pinconf-generic.h>
#include <linux/pinctrl/pinmux.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/sys_config.h>
#include <linux/slab.h>
#include <linux/mfd/core.h>
#include <linux/seq_file.h>
#include <linux/i2c.h>
#include "../../pinctrl/core.h"
#include "axp-core.h"
#include "axp-gpio.h"

#define MODULE_NAME "axp-pinctrl"

static struct axp_pinctrl_group *axp_pinctrl_find_group_by_name(
		struct axp_pinctrl *pctl, const char *group)
{
	int i;

	for (i = 0; i < pctl->ngroups; i++) {
		struct axp_pinctrl_group *grp = pctl->groups + i;

		if (!strcmp(grp->name, group))
			return grp;
	}

	return NULL;
}

static struct axp_desc_function *axp_pinctrl_desc_find_function_by_name(
				struct axp_pinctrl *pctl,
				const char *pin_name,
				const char *func_name)
{
	int i;

	for (i = 0; i < pctl->desc->npins; i++) {
		const struct axp_desc_pin *pin = pctl->desc->pins + i;
		if (!strcmp(pin->pin.name, pin_name)) {
			struct axp_desc_function *func = pin->functions;
			while (func->name) {
				if (!strcmp(func->name, func_name))
					return func;
				func++;
			}
		}
	}

	return NULL;
}

static int axp_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
{
	struct axp_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);

	return pctl->ngroups;
}

static const char *axp_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
					unsigned selector)
{
	struct axp_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);

	return pctl->groups[selector].name;
}

static int axp_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
					unsigned selector,
					const unsigned **pins,
					unsigned *num_pins)
{
	struct axp_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);

	*pins = (unsigned *)&pctl->groups[selector].pin;
	*num_pins = 1;

	return 0;
}

static int axp_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
					struct device_node *node,
					struct pinctrl_map **map,
					unsigned *num_maps)
{
	struct axp_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
	unsigned long *pinconfig;
	struct property *prop;
	const char *function;
	const char *group;
	int ret, nmaps, i = 0;
	u32 val;

	pr_debug("%s enter...\n", __func__);

	*map = NULL;
	*num_maps = 0;

	ret = of_property_read_string(node, "allwinner,function", &function);
	if (ret) {
		dev_err(pctl->dev,
		"missing allwinner, function property in node %s\n",
		node->name);
		return -EINVAL;
	}

	nmaps = of_property_count_strings(node, "allwinner,pins") * 2;
	if (nmaps < 0) {
		dev_err(pctl->dev,
		"missing allwinner,pins property in node %s\n",
		node->name);
		return -EINVAL;
	}

	*map = kmalloc(nmaps * sizeof(struct pinctrl_map), GFP_KERNEL);
	if (!map)
		return -ENOMEM;

	of_property_for_each_string(node, "allwinner,pins", prop, group) {
		struct axp_pinctrl_group *grp =
			axp_pinctrl_find_group_by_name(pctl, group);
		int configlen = 0;

		if (!grp) {
			dev_err(pctl->dev, "unknown pin %s", group);
			continue;
		}

		if (!axp_pinctrl_desc_find_function_by_name(pctl,
			grp->name, function)) {
			dev_err(pctl->dev,
			"unsupported function %s on pin %s",
			function, group);
			continue;
		}

		(*map)[i].type = PIN_MAP_TYPE_MUX_GROUP;
		(*map)[i].data.mux.group = group;
		(*map)[i].data.mux.function = function;

		i++;

		(*map)[i].type = PIN_MAP_TYPE_CONFIGS_GROUP;
		(*map)[i].data.configs.group_or_pin = group;

		if (of_find_property(node, "allwinner,drive", NULL))
			configlen++;
		if (of_find_property(node, "allwinner,pull", NULL))
			configlen++;

		pinconfig = kzalloc(configlen * sizeof(*pinconfig), GFP_KERNEL);

		if (!of_property_read_u32(node, "allwinner,drive", &val))
			;

		if (!of_property_read_u32(node, "allwinner,pull", &val))
			;

		(*map)[i].data.configs.configs = pinconfig;
		(*map)[i].data.configs.num_configs = configlen;

		i++;
	}

	*num_maps = nmaps;

	return 0;
}

static void axp_pctrl_dt_free_map(struct pinctrl_dev *pctldev,
					struct pinctrl_map *map,
					unsigned num_maps)
{
	int i;

	for (i = 0; i < num_maps; i++) {
		if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP)
			kfree(map[i].data.configs.configs);
	}

	kfree(map);
}

static struct pinctrl_ops axp_pinctrl_ops = {
	.dt_node_to_map   = axp_pctrl_dt_node_to_map,
	.dt_free_map      = axp_pctrl_dt_free_map,
	.get_groups_count = axp_pinctrl_get_groups_count,
	.get_group_name   = axp_pinctrl_get_group_name,
	.get_group_pins   = axp_pinctrl_get_group_pins,
};

static int axp_pmx_get_functions_count(struct pinctrl_dev *pctldev)
{
	struct axp_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);

	return pctl->nfunctions;
}

static const char *axp_pmx_get_function_name(struct pinctrl_dev *pctldev,
				unsigned selector)
{
	struct axp_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);

	return pctl->functions[selector].name;
}

static int axp_pmx_get_function_groups(struct pinctrl_dev *pctldev,
					unsigned function,
					const char * const **groups,
					unsigned * const num_groups)
{
	struct axp_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);

	*groups     = pctl->functions[function].groups;
	*num_groups = pctl->functions[function].ngroups;

	return 0;
}

static int axp_pmx_enable(struct pinctrl_dev *pctldev,
					unsigned function,
					unsigned group)
{
	struct axp_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
	struct axp_pinctrl_group *g = pctl->groups + group;
	struct axp_pinctrl_function *func = pctl->functions + function;
	struct axp_desc_function *desc =
	axp_pinctrl_desc_find_function_by_name(pctl, g->name, func->name);

	if (!desc)
		return -EINVAL;

	pr_debug("%s enter...\n", __func__);

	pctl->ops->pmx_set(pctl->axp_dev, g->pin, desc->muxval);

	return 0;
}

static void axp_pmx_disable(struct pinctrl_dev *pctldev,
							unsigned function,
							unsigned group)
{
	return;
}

static int axp_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
					struct pinctrl_gpio_range *range,
					unsigned offset,
					bool input)
{
	struct axp_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
	struct axp_desc_function *desc;
	char        pin_name[AXP_PIN_NAME_MAX_LEN];
	const char *func;
	int         ret;

	pr_debug("%s enter...\n", __func__);

	ret = sprintf(pin_name, "GPIO%d", offset);
	if (!ret)
		goto error;

	if (input)
		func = "gpio_in";
	else
		func = "gpio_out";

	desc = axp_pinctrl_desc_find_function_by_name(pctl, pin_name, func);
	if (!desc) {
		ret = -EINVAL;
		goto error;
	}

	pctl->ops->pmx_set(pctl->axp_dev, offset, desc->muxval);
	ret = 0;

error:
	return ret;
}

static struct pinmux_ops axp_pmx_ops = {
	.get_functions_count = axp_pmx_get_functions_count,
	.get_function_name   = axp_pmx_get_function_name,
	.get_function_groups = axp_pmx_get_function_groups,
	.enable              = axp_pmx_enable,
	.disable             = axp_pmx_disable,
	.gpio_set_direction  = axp_pmx_gpio_set_direction,
};

static int axp_pinconf_get(struct pinctrl_dev *pctldev,
						unsigned pin,
						unsigned long *config)
{
	struct axp_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
	int                 data;

	pr_debug("%s enter... pin = %d\n", __func__, pin);

	switch (SUNXI_PINCFG_UNPACK_TYPE(*config)) {
	case SUNXI_PINCFG_TYPE_DAT:
		data = pctl->ops->gpio_get_data(pctl->axp_dev, pin);
		*config = SUNXI_PINCFG_PACK(SUNXI_PINCFG_TYPE_DAT, data);
		pr_debug("axp pconf get pin [%s] data [%d]\n",
				 pin_get_name(pctl->pctl_dev, pin), data);
		break;
	case SUNXI_PINCFG_TYPE_FUNC:
		data = pctl->ops->pmx_get(pctl->axp_dev, pin);
		*config = SUNXI_PINCFG_PACK(SUNXI_PINCFG_TYPE_FUNC, data);
		pr_debug("axp pconf get pin [%s] funcs [%d]\n",
				 pin_get_name(pctl->pctl_dev, pin), data);
		break;
	default:
		pr_debug("invalid axp pconf type for get\n");
		return -EINVAL;
	}

	return 0;
}

static int axp_pinconf_set(struct pinctrl_dev *pctldev,
					unsigned pin,
					unsigned long *pin_config,
					unsigned num_configs)
{
	struct axp_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
	unsigned long config = (unsigned long)pin_config;
	int                  data;
	int                  func;

	pr_debug("%s enter... pin = %d\n", __func__, pin);

	switch (SUNXI_PINCFG_UNPACK_TYPE(config)) {
	case SUNXI_PINCFG_TYPE_DAT:
		data = SUNXI_PINCFG_UNPACK_VALUE(config);
		pctl->ops->gpio_set_data(pctl->axp_dev, pin, data);
		pr_debug("axp pconf set pin [%s] data to [%d]\n",
				 pin_get_name(pctl->pctl_dev, pin), data);
		break;
	case SUNXI_PINCFG_TYPE_FUNC:
		func = SUNXI_PINCFG_UNPACK_VALUE(config);
		pctl->ops->pmx_set(pctl->axp_dev, pin, func);
		pr_debug("axp pconf set pin [%s] func to [%d]\n",
				 pin_get_name(pctl->pctl_dev, pin), func);
		break;
	default:
		pr_debug("invalid axp pconf type for set\n");
		return -EINVAL;
	}

	return 0;
}

static int axp_pinconf_group_get(struct pinctrl_dev *pctldev,
					unsigned group,
					unsigned long *config)
{
	struct axp_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);

	*config = pctl->groups[group].config;
	return 0;
}

static int axp_pinconf_group_set(struct pinctrl_dev *pctldev,
					unsigned group,
					unsigned long *configs,
					unsigned num_configs)
{
	struct axp_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
	struct axp_pinctrl_group *g = &pctl->groups[group];
	int i = 0;

	for (i = 0; i < num_configs; i++) {
		/* cache the config value */
		g->config = configs[i];
	}

	return 0;
}

static struct pinconf_ops axp_pinconf_ops = {
	.pin_config_get       = axp_pinconf_get,
	.pin_config_set       = axp_pinconf_set,
	.pin_config_group_get = axp_pinconf_group_get,
	.pin_config_group_set = axp_pinconf_group_set,
};

static int axp_gpio_request(struct gpio_chip *chip, unsigned offset)
{
	return pinctrl_request_gpio(chip->base + offset);
}

static void axp_gpio_free(struct gpio_chip *chip, unsigned offset)
{
	pinctrl_free_gpio(chip->base + offset);
}

static int axp_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
{
	return pinctrl_gpio_direction_input(chip->base + offset);
}

static int axp_gpio_get(struct gpio_chip *chip, unsigned offset)
{
	struct axp_pinctrl *pctl = container_of(chip,
				struct axp_pinctrl, gpio_chip);
	return pctl->ops->gpio_get_data(pctl->axp_dev, offset);
}

static void axp_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
{
	struct axp_pinctrl *pctl = container_of(chip,
				struct axp_pinctrl, gpio_chip);
	pctl->ops->gpio_set_data(pctl->axp_dev, offset, value);
}

static int axp_gpio_direction_output(struct gpio_chip *chip,
					unsigned offset, int value)
{
	return pinctrl_gpio_direction_output(chip->base + offset);
}

static int axp_pinctrl_gpio_of_xlate(struct gpio_chip *gc,
					const struct of_phandle_args *gpiospec,
					u32 *flags)
{
	struct gpio_config *config = NULL;
	int pin, base;

	pr_debug("%s enter... gpiospec->args[0] = %d,\"\
			+ \" gpiospec->args[1] = %d\n",
			__func__, gpiospec->args[0], gpiospec->args[1]);

	base = AXP_PIN_BASE;
	pin = base + gpiospec->args[1];

	pin = pin-gc->base;
	if (pin > gc->ngpio)
		return -EINVAL;

	if (flags) {
		config = (struct gpio_config *)flags;
		config->gpio = base + gpiospec->args[1];
		config->mul_sel = gpiospec->args[2];
		config->drv_level = gpiospec->args[3];
		config->pull = gpiospec->args[4];
		config->data = gpiospec->args[5];
	}

	return pin;
}

static int axp_pinctrl_add_function(struct axp_pinctrl *pctl, const char *name)
{
	struct axp_pinctrl_function *func = pctl->functions;

	while (func->name) {
		/* function already there */
		if (strcmp(func->name, name) == 0) {
			func->ngroups++;
			return -EEXIST;
		}
		func++;
	}

	func->name = name;
	func->ngroups = 1;

	pctl->nfunctions++;

	return 0;
}

static struct axp_pinctrl_function *axp_pinctrl_find_function_by_name(
		struct axp_pinctrl *pctl, const char *name)
{
	int i;
	struct axp_pinctrl_function *func = pctl->functions;

	for (i = 0; i < pctl->nfunctions; i++) {
		if (!func[i].name)
			break;

		if (!strcmp(func[i].name, name))
			return func + i;
	}

	return NULL;
}

static int axp_pinctrl_build_state(struct device *dev, struct axp_pinctrl *pctl)
{
	int i;

	pctl->ngroups = pctl->desc->npins;
	/* Allocate groups */
	pctl->groups = devm_kzalloc(dev,
		pctl->ngroups * sizeof(*pctl->groups), GFP_KERNEL);
	if (!pctl->groups)
		return -ENOMEM;

	for (i = 0; i < pctl->desc->npins; i++) {
		const struct axp_desc_pin *pin = pctl->desc->pins + i;
		struct axp_pinctrl_group *group = pctl->groups + i;

		group->name = pin->pin.name;
		group->pin = pin->pin.number;
	}

	/*
	 * We suppose that we won't have any more functions than pins,
	 * we'll reallocate that later anyway
	 */
	pctl->functions = devm_kzalloc(dev,
		pctl->desc->npins * sizeof(*pctl->functions),
		GFP_KERNEL);
	if (!pctl->functions)
		return -ENOMEM;

	/* Count functions and their associated groups */
	for (i = 0; i < pctl->desc->npins; i++) {
		const struct axp_desc_pin *pin = pctl->desc->pins + i;
		struct axp_desc_function *func = pin->functions;

		while (func->name) {
			axp_pinctrl_add_function(pctl, func->name);
			func++;
		}
	}

	pctl->functions = krealloc(pctl->functions,
		pctl->nfunctions * sizeof(*pctl->functions),
		GFP_KERNEL);

	for (i = 0; i < pctl->desc->npins; i++) {
		const struct axp_desc_pin *pin = pctl->desc->pins + i;
		struct axp_desc_function *func = pin->functions;

		while (func->name) {
			struct axp_pinctrl_function *func_item;
			const char **func_grp;

			func_item = axp_pinctrl_find_function_by_name(
				pctl, func->name);
			if (!func_item)
				return -EINVAL;

			if (!func_item->groups) {
				func_item->groups = devm_kzalloc(dev,
				func_item->ngroups * sizeof(*func_item->groups),
				GFP_KERNEL);
				if (!func_item->groups)
					return -ENOMEM;
			}

			func_grp = func_item->groups;
			while (*func_grp)
				func_grp++;

			*func_grp = pin->pin.name;
			func++;
		}
	}

	return 0;
}

struct axp_pinctrl *axp_pinctrl_register(struct device *dev,
					struct axp_dev *axp_dev,
					struct axp_pinctrl_desc *desc,
					struct axp_gpio_ops *ops)
{
	struct pinctrl_desc *pctrl_desc;
	struct axp_pinctrl      *pctl;
	struct pinctrl_pin_desc *pins;
	int    ret;
	int    i, rc;

	/* allocate and initialize axp-pinctrl */
	pr_debug("axp_pinctrl_probe enter...\n");
	pctl = devm_kzalloc(dev, sizeof(*pctl), GFP_KERNEL);
	if (!pctl) {
		dev_err(dev,
		"allocate memory for axp-pinctrl structure failed\n");
		return NULL;
	}

	pctl->dev  = dev;
	pctl->desc = desc;
	pctl->ops  = ops;
	pctl->axp_dev = axp_dev;

	ret = axp_pinctrl_build_state(dev, pctl);
	if (ret) {
		dev_err(dev, "dt probe failed: %d\n", ret);
		return NULL;
	}

	/* register axp pinctrl */
	pins = devm_kzalloc(dev, pctl->desc->npins * sizeof(*pins), GFP_KERNEL);
	if (!pins)
		return NULL;

	for (i = 0; i < pctl->desc->npins; i++)
		pins[i] = pctl->desc->pins[i].pin;

	pctrl_desc = devm_kzalloc(dev, sizeof(*pctrl_desc), GFP_KERNEL);
	if (!pctrl_desc)
		return NULL;

	pctrl_desc->name = dev_name(dev);
	pctrl_desc->owner = THIS_MODULE;
	pctrl_desc->pins = pins;
	pctrl_desc->npins = pctl->desc->npins;
	pctrl_desc->pctlops = &axp_pinctrl_ops,
	pctrl_desc->pmxops  = &axp_pmx_ops,
	pctrl_desc->confops = &axp_pinconf_ops,

	pctl->pctl_dev = pinctrl_register(pctrl_desc, dev, pctl);
	if (!pctl->pctl_dev) {
		rc = gpiochip_remove(&pctl->gpio_chip);
		if (rc < 0)
			return NULL;
		return NULL;
	}

	/* initialize axp-gpio-chip */
	pctl->gpio_chip.dev              = dev;
	pctl->gpio_chip.label            = MODULE_NAME;
	pctl->gpio_chip.owner            = THIS_MODULE;
	pctl->gpio_chip.request          = axp_gpio_request;
	pctl->gpio_chip.free             = axp_gpio_free;
	pctl->gpio_chip.direction_input  = axp_gpio_direction_input;
	pctl->gpio_chip.direction_output = axp_gpio_direction_output;
	pctl->gpio_chip.get              = axp_gpio_get;
	pctl->gpio_chip.set              = axp_gpio_set;
	pctl->gpio_chip.of_xlate         = axp_pinctrl_gpio_of_xlate;
	pctl->gpio_chip.of_gpio_n_cells  = 6;
	pctl->gpio_chip.base             = AXP_PIN_BASE;
	pctl->gpio_chip.ngpio            = desc->npins;
	pctl->gpio_chip.can_sleep        = 0,

	ret = gpiochip_add(&pctl->gpio_chip);
	if (ret) {
		dev_err(dev, "could not add GPIO chip\n");
		return NULL;
	}

	for (i = 0; i < pctl->desc->npins; i++) {
		const struct axp_desc_pin *pin = pctl->desc->pins + i;

		ret = gpiochip_add_pin_range(&pctl->gpio_chip, dev_name(dev),
					pin->pin.number,
					pin->pin.number, 1);
		if (ret) {
			dev_err(dev, "could not add GPIO pin range\n");
			rc = gpiochip_remove(&pctl->gpio_chip);
			if (rc < 0)
				return NULL;
			return NULL;
		}
	}

	pr_debug("axp pinctrl driver register ok\n");
	return pctl;
}
EXPORT_SYMBOL_GPL(axp_pinctrl_register);

int axp_pinctrl_unregister(struct axp_pinctrl *pctl)
{
	int rc;

	pinctrl_unregister(pctl->pctl_dev);
	rc = gpiochip_remove(&pctl->gpio_chip);
	if (rc < 0)
		return rc;

	return 0;
}
EXPORT_SYMBOL_GPL(axp_pinctrl_unregister);

struct axp_gpio_irq_ops axp_irq_ops[AXP_ONLINE_SUM];

void axp_gpio_irq_ops_set(int pmu_num, struct axp_gpio_irq_ops *ops)
{
	axp_irq_ops[pmu_num].irq_request  = ops->irq_request;
	axp_irq_ops[pmu_num].irq_free     = ops->irq_free;
	axp_irq_ops[pmu_num].irq_ack      = ops->irq_ack;
	axp_irq_ops[pmu_num].irq_enable   = ops->irq_enable;
	axp_irq_ops[pmu_num].irq_disable  = ops->irq_disable;
	axp_irq_ops[pmu_num].irq_set_type = ops->irq_set_type;
}

int axp_gpio_irq_request(int pmu_num, int gpio_no,
				u32 (*handler)(int, void *), void *data)
{
	return axp_irq_ops[pmu_num].irq_request(gpio_no, handler, data);
}
EXPORT_SYMBOL_GPL(axp_gpio_irq_request);

int axp_gpio_irq_enable(int pmu_num, int gpio_no)
{
	return axp_irq_ops[pmu_num].irq_enable(gpio_no);
}
EXPORT_SYMBOL_GPL(axp_gpio_irq_enable);

int axp_gpio_irq_disable(int pmu_num, int gpio_no)
{
	return axp_irq_ops[pmu_num].irq_disable(gpio_no);
}
EXPORT_SYMBOL_GPL(axp_gpio_irq_disable);

int axp_gpio_irq_set_type(int pmu_num, int gpio_no, unsigned long type)
{
	return axp_irq_ops[pmu_num].irq_set_type(gpio_no, type);
}
EXPORT_SYMBOL_GPL(axp_gpio_irq_set_type);

int axp_gpio_irq_free(int pmu_num, int gpio_no)
{
	return axp_irq_ops[pmu_num].irq_free(gpio_no);
}
EXPORT_SYMBOL_GPL(axp_gpio_irq_free);

int axp_gpio_irq_valid(struct axp_pinctrl_desc *desc, int gpio_no)
{
	int i;
	struct axp_desc_function *func;

	for (i = 0; i < desc->npins; i++) {
		func = (desc->pins + i)->functions;
		while (func->name) {
			if (!strcmp(func->name, "irq")) {
				if (i == gpio_no)
					goto found;
			}
			func++;
		}
	}

	return 0;

found:
	return func->irq_valid;
}
